package cn.yunhe.servlet;

import cn.yunhe.bean.User;
import cn.yunhe.biz.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/UserRegServlet")
public class UserRegServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("username");
        String pwd = request.getParameter("password");
        String realname = request.getParameter("realname");
        Long phone = Long.parseLong(request.getParameter("phone"));
        String email = request.getParameter("email");
        String sex = request.getParameter("sex");

        User user = new User(realname,name,pwd,phone,email,sex);
        UserService userService = new UserService();
        int flag = userService.addUser(user);
        PrintWriter out =response.getWriter();
        if (flag > 0){
            out.print("<script>alert('注册成功');location.href='userLogin.jsp'</script>");
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
